import success from "./success.json";

export function findItem(items: Array<any>, id: string): any {
    for (let i = 0; i < items.length; i++) {
        if (items[i].id == id) {
            //console.log(items[i])
            return items[i];
        }
        if (items[i].children) {
            let result = findItem(items[i].children, id);
            if (result) {
                return result;
            }
        }
    }

    return null;
}

export function findItemById(items: Array<any>, id: string, attrName: string): any {
    for (let i = 0; i < items.length; i++) {
        if (items[i][attrName] == id) {
            //console.log(items[i])
            return items[i];
        }
        if (items[i].children) {
            let result = findItemById(items[i].children, id, attrName);
            if (result) {
                return result;
            }
        }
    }

    return null;
}

export function findDetailFromList(items: Array<any>, id: string, attrName: string): any {
    let info = findItemById(items, id, attrName);
    let infoResult = null;
    if (info) {
        infoResult = JSON.parse(JSON.stringify(info));
    }
    if (info && info.children) {
        infoResult.children = null;
    }

    let result = JSON.parse(JSON.stringify(success));
    // @ts-ignore
    result.data = infoResult;

    return result;
}

/**
 * 从URL取得path值
 * @param fullRex 原占位符
 * @param fullUrl  完整的url
 * @param urlVar 参数名
 */
export function getUrlPath(fullRex: string, fullUrl: string, urlVar: string): string {

    fullUrl = fullUrl.split("?")[0];
    let fullUrls = fullUrl.split("/");
    let fullRexs = fullRex.split("/");

    for (let i = 0; i < fullUrls.length; i++) {
        if (fullRexs[i] == ":" + urlVar) {
            return fullUrls[i];
        }
    }
    return "";
}
